//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageIndex: 1,
    listData: [],
    state: '',
    modalFlag: true,
    tiXianMoney:"",
    // 用户信息
    uid:"",
    myRMB:"",
    userTrueName:"",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
    })
    this.GetMyRMBRecord();
    this.GetUserInfoByIDs();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.pageIndex++;
    this.GetMyRMBRecord();
  },
  GetMyRMBRecord: function() {
    var that = this;
    var uid = this.data.uid;
    var pageIndex = this.data.pageIndex;
    var listData = this.data.listData;
    if (pageIndex == 1) {
      wx.showLoading({
        title: '加载中',
      })
    }
    wx.request({
      url: baseJs.api + baseJs.encryUrl('method=GetMyPayRecord&UID=' + uid + "&PageSize=10&PageIndex=" + pageIndex + "&FlowType=&Type=3"),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        for (var i = 0; i < json.length; i++) {
          json[i].CreateTime = json[i].CreateTime.replace("T", " ");
          listData.push(json[i])
        }
        that.setData({
          listData: listData,
        })
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
        wx.hideLoading();
      }
    })
  },
  // tiXian
  tiXian: function() {
    if (this.data.userTrueName == ''){
      wx.showToast({
        title: "请先去完善个人信息！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    this.setData({
      modalFlag: false,
    })
  },
  //tiXianCancel
  tiXianCancel: function () {
    this.setData({
      modalFlag: true,
    })
  },
  // tiXianInput
  tiXianInput:function(e){
    this.setData({
      tiXianMoney: e.detail.value
    });
  },
  // tiXianConfirm
  tiXianConfirm: function () {
    this.setData({
      modalFlag: true,
    })
    var that = this;
    var uid = this.data.uid;
    var tiXianMoney = this.data.tiXianMoney;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CarryMoney&UID=" + uid + "&CarryType=1&Money=" + tiXianMoney),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        wx.showToast({
          title: json.ResultMsg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  // 
  GetUserInfoByIDs: function () {
    var that = this;
    var uid = this.data.uid;
    var myRMB = this.data.myRMB;
    var userTrueName = this.data.userTrueName;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetUserInfoByIDs&UID=" + uid),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        userTrueName = json.ResultData.TrueName;
        myRMB = parseFloat(json.ResultData.MyRMB).toFixed(2);
        that.setData({
          myRMB: myRMB,
          userTrueName: userTrueName,
        })
      }
    })
  }
})